package export.strength
{
    import com.dusk.tool.SubWindow;
    import com.dusk.util.StringUtil;
    import com.game.data.constant.ConfigConst;
    import com.game.manager.NotificationManager;
    import com.game.manager.SubWindowManager;
    import com.hurlant.util.Base64;
    
    import config.Config;
    
    import flash.events.Event;
    import flash.events.FocusEvent;
    import flash.text.TextField;
    import flash.text.TextFieldType;
    import flash.text.TextFormat;
    
    public class FusionHelp
	{
		public function FusionHelp()
		{
			if (SubWindowManager.getIns().hasRegisterWindow("合成图鉴"))
			{
				this.allInfo = "";
				NotificationManager.alert.alert("图鉴已经打开");
				return;
			}
			this.refreshInfo();
			this.initTextFormat();
			this.initWindow();
		}
		
		private var allInfo:String = "合成公式:\n\n";
		private var infoTxt:TextField;
		private var searchTxt:TextField;
		private var tf1:TextFormat;
		private var tf2:TextFormat;
		
		private function refreshInfo():void
		{
			var Items:Vector.<String> = Config.getIns().allEquip.getFusionData();
			for each (var item:String in Items)
			{
				var _loc1_:String = Base64.decode(item);
				var input:Array = _loc1_.split("|")[0].split(";");
				var output:Array = _loc1_.split("|")[1].split(";");
				for (var a:int in input)
				{
					this.allInfo += Config.getIns().allEquip.findByName(input[a]).name;
					if (a < 2)
					{
						this.allInfo += " + ";
					}
					else
					{
						this.allInfo += " = ";
					}
				}
				this.allInfo += output[1];
				this.allInfo += "(" + output[2] + "%)";
				this.allInfo += "\n";
			}
		}
		
		private function initTextFormat():void
		{
			tf1 = new TextFormat(ConfigConst.GAME_CONFIG_FONT, 15, 0xFFFFFF);
			tf2 = new TextFormat(ConfigConst.GAME_CONFIG_FONT, 15, 0x00FFFF);
		}
		
		private function initWindow():void
		{
			var a:uint = 500;
			
			infoTxt = new TextField();
			infoTxt.embedFonts = true;
			infoTxt.x = infoTxt.y = 0;
			infoTxt.selectable = true;
			infoTxt.defaultTextFormat = tf1;
			infoTxt.setTextFormat(tf1);
			infoTxt.text = this.allInfo;
			infoTxt.height = a;
			infoTxt.width = infoTxt.textWidth + 5;
			
			searchTxt = new TextField();
			searchTxt.embedFonts = true;
			searchTxt.x = 0;
			searchTxt.y = a;
			searchTxt.type = TextFieldType.INPUT;
			searchTxt.text = "查找";
			searchTxt.defaultTextFormat = tf2;
			searchTxt.setTextFormat(tf2);
			searchTxt.addEventListener(Event.CHANGE, this.setResearchContent);
			searchTxt.addEventListener(FocusEvent.FOCUS_OUT, this.setResearchContent);
			
			var window:SubWindow = new SubWindow("合成图鉴",infoTxt.width + 5,a + 100);
			window.stage.addChild(infoTxt);
			window.stage.addChild(searchTxt);
			window.setCloseFunc(this.close);
			window.activate();
		}
		
		private function setResearchContent(param1:Event):void
		{
			this.infoTxt.setTextFormat(tf1);
			if (this.searchTxt.text == "")
			{
				if (param1.type == FocusEvent.FOCUS_OUT)
				{
					this.searchTxt.appendText("查找");
				}
				return;
			}
			var indexArr:Array = StringUtil.findAll(searchTxt.text,infoTxt.text);
			while (indexArr.length)
			{
				var k:int = indexArr.pop();
				if (k == -1)
				{
					continue;
				}
				this.infoTxt.setTextFormat(tf2, k, k + this.searchTxt.text.length);
				this.infoTxt.setSelection(k, k + this.searchTxt.text.length);
			}
		}
		
		private function close():void
		{
			trace("fusion window closed");
			this.searchTxt.removeEventListener(FocusEvent.FOCUS_OUT, this.setResearchContent);
			this.searchTxt.removeEventListener(Event.CHANGE, this.setResearchContent);
			this.allInfo = "";
		}
	}
}
